package com.rf.richfitwheel.admin.sys.controller;

import com.rf.richfitwheel.admin.contants.UrlConstant;
import com.rf.richfitwheel.admin.sys.model.OrgRoles;
import com.rf.richfitwheel.admin.sys.model.SysMsgStream;
import com.rf.richfitwheel.admin.sys.service.SysMsgStreamService;
import com.rf.richfitwheel.admin.sys.vo.UserVO;
import com.rf.richfitwheel.admin.utils.UserUtil;
import com.rf.richfitwheel.common.exception.BusinessException;
import com.rf.richfitwheel.common.utils.PageUtils;
import com.rf.richfitwheel.common.utils.R;
import com.rf.richfitwheel.common.utils.StringUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping(UrlConstant.WEB_ADMIN_URL+"/msgStream")
public class SysMsgStreamController {

	private static Logger log = LoggerFactory.getLogger(SysMsgStreamController.class);
	
    @Autowired
    private SysMsgStreamService msgStreamService;

    /**
     * 发送消息接口
     * @param params {"senderId":发送用户id(不要用员工编号）,"receiverId":消息接收人id,"pageParam":页面参数,"msgCode":消息定义编码,"businessId":业务id,"msgContext":消息内容,"msgContextParam":消息内容参数}
     * @return
     */
    @PostMapping("/sendMsg")
    public boolean sendMsg(@RequestBody Map<String, Object> params){
        try {
            if (StringUtils.isBlank(params.get("senderId"))){
                try {
                    UserVO userVO = UserUtil.getUser();
                    params.put("senderId", userVO.getId());
                } catch (Exception e) {
                    params.put("senderId", UserUtil.getSystem().getId());
                }
            }
            return msgStreamService.sendMsg(params);
        } catch (Exception e) {
            log.error("发送消息失败", e);
            return false;
        }
    }

    /**
     * 所有配置列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = msgStreamService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 当前登录用户的未读消息
     * @param params
     * @return
     */
    @GetMapping("/queryUndealMsg")
    public R queryUndealMsg(@RequestParam Map<String, Object> params){
    	UserVO userVO = UserUtil.getUser();
        PageUtils page = msgStreamService.queryUndealMsg(params, userVO);
        return R.ok().put("undealMsg", page);
    }

    /**
     * 配置信息
     * @throws Exception
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") String id) throws Exception{
        SysMsgStream msgStream=msgStreamService.selectMapById(id);
        return R.ok().put("msgStream", msgStream);
    }

    /**
     * 保存配置
     */
    @PostMapping("/save")
    public R save(@RequestBody SysMsgStream msgStream){
        msgStreamService.save(msgStream);
        return R.ok();
    }

    /**
     * 修改配置
     */
    @PostMapping("/update")
    public R update(@RequestBody SysMsgStream msgStream){
        msgStreamService.update(msgStream);
        return R.ok();
    }

    /**
     * 删除配置
     */
    @PostMapping("/delete")
    public R delete(@RequestBody String[] ids){
        msgStreamService.deleteBatch(ids);
        return R.ok();
    }

    /**
     * 标为已读
     */
    @PostMapping("/deal")
    public R deal(@RequestBody String[] ids){
        msgStreamService.deal(ids);
        return R.ok();
    }

    /**
     * 查询待处理的消息数量
     * @param staffId
     * @return
     */
    @GetMapping("/getUndealMsgNum/{staffId}/{tenantId}")
    public R getUndealMsgNum(@PathVariable("staffId") String staffId,@PathVariable("tenantId") String tenantId){
        int msgNum = msgStreamService.getUndealMsgNum(staffId,tenantId);
        return R.ok().put("msgNum",msgNum);
    }

    /**
     * 查询当前用户待处理的消息数量
     * @return
     */
    @GetMapping("/getUndealMsgNumSession")
    public R getUndealMsgNumSession(String tenantId){
        UserVO user = UserUtil.getUser();
        int msgNum = msgStreamService.getUndealMsgNum(user.getId(),tenantId);
        return R.ok().put("msgNum",msgNum);
    }
    
    /**
     * 给下级机构的角色发消息
     * @param orgRoles 机构角色信息
     * @return 发送结果
     */
    @PostMapping("/sendMsgToChildrenOrgRole")
    public R sendMsgToChildrenOrgRole(@RequestBody OrgRoles orgRoles){
        try{
            msgStreamService.sendMsgToChildrenOrgRole(orgRoles.getOrgCode(), orgRoles.getRoleIds(), orgRoles.getMessage(), orgRoles.getBusinessId());
            return R.ok();
        }catch(BusinessException ex){
            log.error(ex.getMessage(), ex);
            return R.error(ex.getMessage());
        }catch(Exception e){
            log.error("给下级机构的角色发消息失败");
            return R.error("给下级机构的角色发消息失败");
        }
    }
}
